package com.jit.app.model;

import android.os.Handler;
import android.os.Message;

import com.alibaba.fastjson.JSONObject;
import com.jit.app.util.JSONResult;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AppInfoModel {

    private void  load(final Handler handler, String url, final int what){
        //1. 创建OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //2. 创建一个请求信息对象Request
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        //3. 发送请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            //请求失败
            @Override
            public void onFailure(Call call, IOException e) {

            }

            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //response对象中就有结果数据
                String json = response.body().string();//json字符串结果
                JSONResult jsonResult = JSONObject.parseObject(json, JSONResult.class);
                Message message = new Message();
                message.what = what;
                message.obj = jsonResult;
                if(handler != null){
                    handler.sendMessage(message);
                }

            }
        });
    }

    private void  loadpost(final Handler handler, String url,String param, final int what){
        //1. 创建OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //2. 创建一个请求信息对象Request
        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), param);
        Request request = new Request.Builder()
                .post(body)
                .url(url)
                .build();
        //3. 发送请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            //请求失败
            @Override
            public void onFailure(Call call, IOException e) {

            }

            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //response对象中就有结果数据
                String json = response.body().string();//json字符串结果
                JSONResult jsonResult = JSONObject.parseObject(json, JSONResult.class);
                Message message = new Message();
                message.what = what;
                message.obj = jsonResult;
                handler.sendMessage(message);
            }
        });
    }
    //调用应用列表接口
    public void loadAppInfoList(final Handler handler){
        String url = Constants.HOST.API_HOST+ Constants.ApiURI.URI_GETAPPINFOLIST;
        load(handler, url, Constants.MessageWhat.APPINFO_LOAD_SUCCESS);
    }
    //调用轮播图API接口
    public void loadAdPromotionList(final Handler handler){
        String url = Constants.HOST.API_HOST+Constants.ApiURI.URI_GETADLIST;
        load(handler, url,Constants.MessageWhat.ADPROMOTION_LOAD_SUCCESS);
    }

    public void loadAppInfoDetail(final Handler handler,long id){
        String url = Constants.HOST.API_HOST+Constants.ApiURI.URI_GETAPPDETAIL + "&id=" +id;
        load(handler, url,Constants.MessageWhat.APPINFO_LOAD_SUCCESS);
    }


    public void loadCommentScoreList(final Handler handler , long id){
        String url = Constants.HOST.API_HOST+Constants.ApiURI.URI_COMMENTSTATISTIC + "&appId="+id;
        load(handler, url, Constants.MessageWhat.COMMENTSTATISTIC_LOAD_SUCCESS);
    }


    public void loadCommentList(final Handler handler , long id){
        String url = Constants.HOST.API_HOST+Constants.ApiURI.URI_COMMENTLIST + "&appId="+id;
        load(handler, url, Constants.MessageWhat.COMMENTLIST_LOAD_SUCCESS);

    }


    public void loadReplayList(Handler handler, long commentId){
        String url = Constants.HOST.API_HOST+Constants.ApiURI.URI_REPLAYLIST + "&commentId="+commentId;
        load(handler, url, Constants.MessageWhat.REPLAYLIST_LOAD_SUCCESS);
    }


    public void loadAppUserComment(Handler handler, long appId, long userId){
        String url = Constants.HOST.API_HOST+Constants.ApiURI.URI_USERCOMMENT + "&appId="+appId+"&userId="+userId;
        load(handler, url, Constants.MessageWhat.USERCOMMENT_LOAD_SUCCESS);
    }

    public void login(Handler handler, String username, String password){
        String url = Constants.HOST.API_HOST + Constants.ApiURI.URI_LOGIN;
        loadpost(handler, url, "username="+username+"&userPassword="+password, Constants.MessageWhat.LOGIN_LOAD_SUCCESS);
    }

    public void postComment(Handler handler, long appId, long userId, int stars, String content){
        String url = Constants.HOST.API_HOST + Constants.ApiURI.URI_SAVECOMMENT;
        loadpost(handler,url,"appId="+appId+"&userId="+userId+"&stars="+stars+"&content="+content, Constants.MessageWhat.COMMENT_SAVE_SUCCESS);
    }


    public void loadInstalledAppList(Handler handler, List<String> packs){
        String url = Constants.HOST.API_HOST + Constants.ApiURI.URI_INSTALLEDAPPLIST;
        String param = "";
        for(int i=0;i<packs.size();i++){
            String p = packs.get(i);
            param += "packs="+p;
            if(i < packs.size()-1){
                param += "&";
            }
        }
        loadpost(handler,url,param,Constants.MessageWhat.APPINFO_LOAD_SUCCESS);
    }

    public void loadGameAppInfoList(Handler handler) {
        String url = Constants.HOST.API_HOST+Constants.ApiURI.URI_GETGameAPPINFOLIST + "&type=game";
        load(handler, url, Constants.MessageWhat.GAMEAPPINFO_LOAD_SUCCESS);
    }
}

